package com.ossjk.qlh.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.util.EncryptUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.loginfo.LogModule;
import com.ossjk.qlh.loginfo.LogOperation;
import com.ossjk.qlh.system.entity.Department;
import com.ossjk.qlh.system.entity.Role;
import com.ossjk.qlh.system.entity.User;
import com.ossjk.qlh.system.entity.UserRole;
import com.ossjk.qlh.system.service.IDepartmentService;
import com.ossjk.qlh.system.service.IRoleService;
import com.ossjk.qlh.system.service.IUserRoleService;
import com.ossjk.qlh.system.service.IUserService;
import com.ossjk.qlh.system.vo.UserVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author chair
 * @since 2020-01-30
 */

@LogModule(value = "用户管理")
@Api(tags = "用户管理")
@RestController
@RequestMapping("/system/user")
public class UserController extends BaseController {

	@Autowired
	private IUserService iUserService;
	@Autowired
	private IDepartmentService iDepartmentService;
	@Autowired
	private IUserRoleService iUserRoleService;
	@Autowired
	private IRoleService iRoleService;

	@LogOperation("列表")
	@ApiOperation(value = "列表")
	@PostMapping(value = "/list")
	@RequiresPermissions("00000000")
	public ResponseBean<Map<String, Object>> list(@RequestBody RequestBean requestBean) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
		String name = (String) requestBean.getParam().get("name");
		String mobile = (String) requestBean.getParam().get("mobile");
		String rid = (String) requestBean.getParam().get("rid");
		String did = (String) requestBean.getParam().get("did");
		if (!CommonUtil.isBlank(name)) {
			queryWrapper.like("u.name", name);
		}
		if (!CommonUtil.isBlank(mobile)) {
			queryWrapper.like("u.mobile", mobile);
		}
		if (!CommonUtil.isBlank(rid)) {
			queryWrapper.like("r.id", rid);
		}
		if (!CommonUtil.isBlank(did)) {
			queryWrapper.like("d.id", did);
		}
		queryWrapper.orderByDesc("crtm");
		Map<String, Object> data = new HashMap<>();
		data.put("users", iUserService.pageVo(requestBean.getPage(), queryWrapper));
		data.put("roles", iRoleService.list(new QueryWrapper<Role>().orderByAsc("level", "sort")));
		data.put("departments", iDepartmentService.list(new QueryWrapper<Department>().orderByAsc("level", "sort")));
		return responseSuccess(data);
	}

	@LogOperation("去添加")
	@ApiOperation(value = "去添加")
	@PostMapping(value = "/toSave")
	@RequiresPermissions("000000000000")
	public ResponseBean toSave() {
		Map<String, Object> data = new HashMap<String, Object>();
		QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>();
		queryWrapper.orderByAsc("level", "sort");
		data.put("roles", iRoleService.list(queryWrapper));
		return responseSuccess(data);
	}

	@LogOperation("去编辑")
	@ApiOperation(value = "去编辑")
	@PostMapping(value = "/toUpdate")
	@RequiresPermissions("000000000005")
	public ResponseBean toUpdate(@RequestBody RequestBean requestBean) {
		Map<String, Object> data = new HashMap<String, Object>();
		// 查询用户
		UserVo userVo = iUserService.getInfoById((String) requestBean.getParam().get("id"));
		if (!CommonUtil.isBlank(userVo)) {
			data.put("user", userVo);
		}
		// 查询当前用户的角色
		QueryWrapper<UserRole> queryWrapper = new QueryWrapper<UserRole>();
		UserRole userRole = iUserRoleService.getOne(queryWrapper.eq("uid", requestBean.getParam().get("id")));
		if (!CommonUtil.isBlank(userRole)) {
			data.put("rid", userRole.getRid());
		}

		QueryWrapper<Role> queryWrapper2 = new QueryWrapper<Role>();
		queryWrapper2.orderByAsc("level", "sort");
		data.put("roles", iRoleService.list(queryWrapper2));
		return responseSuccess(data);
	}

	@LogOperation("添加")
	@ApiOperation(value = "添加")
	@PostMapping(value = "/save")
	@RequiresPermissions("000000000000")
	public ResponseBean save(@RequestBody RequestBean<UserVo> requestBean) {
		UserVo userVo = requestBean.getData();
		QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
		queryWrapper.eq("lname", userVo.getLname());
		if (!CommonUtil.isBlank(iUserService.getOne(queryWrapper))) {
			return responseFail("登录名重复。");
		}
		queryWrapper = new QueryWrapper<User>();
		queryWrapper.eq("mobile", userVo.getMobile());
		if (!CommonUtil.isBlank(iUserService.getOne(queryWrapper))) {
			return responseFail("手机重复。");
		}
		// 设置默认密码
		userVo.setPwd(EncryptUtil.MD5("888888"));
		if (iUserService.saveVo(userVo)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@LogOperation("编辑")
	@ApiOperation(value = "编辑")
	@PostMapping(value = "/update")
	@RequiresPermissions("000000000005")
	public ResponseBean update(@RequestBody RequestBean<UserVo> requestBean) {
		UserVo userVo = requestBean.getData();
		if (iUserService.updateVo(userVo)) {
			return responseSuccess();
		} else {
			return responseFail();
		}

	}

	@LogOperation("删除")
	@ApiOperation(value = "删除")
	@PostMapping(value = "/remove")
	@RequiresPermissions("000000000010")
	public ResponseBean delete(@RequestBody RequestBean requestBean) {
		List ids = (List) requestBean.getParam().get("ids");
		if (iUserService.removeUserRoleByIds(ids)) {
			ids.clear();
			return responseSuccess();
		} else {
			return responseFail();
		}
	}
}
